﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using XnaContentManager = Microsoft.Xna.Framework.Content.ContentManager;

namespace WindowsGame.Library.Manager
{
	public class TheContentManager
	{
		private static TheContentManager instance;
		private static IDictionary<String, Object> contentDictionary;

		private XnaContentManager xnaContentManager;
		private IDictionary<String, String> contentPaths;

		public void Initialize(XnaContentManager xnaContentManager, IDictionary<String, String> contentPaths)
		{
			this.xnaContentManager = xnaContentManager;
			this.contentPaths = contentPaths;
		}

		public T Load<T>(String name)
		{
			String type = typeof(T).Name;
			String path = contentPaths[type];

			String assetName = String.Format("{0}/{1}", path, name);
			if (contentDictionary.ContainsKey(assetName))
			{
				Object obj = contentDictionary[assetName];
				return (T)obj;
			}

			T content = xnaContentManager.Load<T>(assetName);
			contentDictionary.Add(assetName, content);

			return content;
		}

		public void Unload()
		{
			xnaContentManager.Unload();
		}

		public static TheContentManager Instance
		{
			get
			{
				if (null == instance)
				{
					instance = new TheContentManager();
				}

				return instance;
			}
		}

		private TheContentManager()
		{
			contentDictionary = new Dictionary<String, Object>();
		}
	}
}